package com.hrm.controller;


import com.hrm.pojo.DTO.userPasswordDTO;
import com.hrm.pojo.Users;
import com.hrm.pojo.DTO.UsersPageDTO;
import com.hrm.pojo.DTO.UsersUpdateDTO;
import com.hrm.service.UserService;
import com.hrm.utils.JwtUtil;
import com.hrm.utils.entity.PageResult;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import com.mysql.cj.xdevapi.JsonString;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/users")
@RestController
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    /**
     * 员工登录
     * @param users
     * @return
     * @throws Exception
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody Users users) throws Exception {
        Users loginUser = userService.login(users.getLoginname(),users.getPassword());
        //使用用户信息生成token
        String token = jwtUtil.createJWT(loginUser.getId() + "", loginUser.getLoginname()+"", loginUser.getStatus() + "");
        System.out.println("token:"+token);
        Map map = new HashMap<>();
        map.put("user",loginUser);
        map.put("token",token);
        return new Result(true, StatusCode.SUCCESS, "操作完成",map);
    }

    /**
     * 员工注册
     * @param users
     * @return
     * @throws Exception
     */
    @ApiOperation("添加用户")
    @PostMapping("/register")
    public Result register(@RequestBody Users users) throws Exception {
        Boolean flag = userService.register(users);
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }
    /**
     * 修改密码
     * @return
     */
    @ApiOperation("修改密码")
    @PutMapping("/rePwd")
    public Result rePwd(@RequestBody userPasswordDTO passwordUpdateDTO) throws Exception {
        Boolean flag = userService.rePwd(passwordUpdateDTO.getLoginname(), passwordUpdateDTO.getOldPwd(), passwordUpdateDTO.getNewPwd());
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }

    /**
     * 用户列表
     * @param usersPageDTO
     * @return
     */
    @ApiOperation("用户列表")
    @GetMapping("/page")
    public Result list( UsersPageDTO usersPageDTO){
        PageResult<Users> pageUsers = userService.page(usersPageDTO);
        return new Result(pageUsers!=null,pageUsers!=null?StatusCode.SUCCESS:StatusCode.ERROR,"操作完成",pageUsers);
    }

    /**
     * 删除用户根据id
     * @param id
     * @return
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        Boolean flag = userService.delete(id);
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }

    /**
     * 修改用户
     * @param
     * @param usersUpdateDTO
     * @return
     */
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result updateUser(@RequestBody UsersUpdateDTO usersUpdateDTO){
        Boolean flag = userService.update(usersUpdateDTO);
        return new Result(flag, flag ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成");
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Integer id){
        Users users = userService.getById(id);
        return new Result(users!=null, users!=null ? StatusCode.SUCCESS : StatusCode.ERROR, "操作完成",users);

    }
}
